package com.futurelight.recipes.repository.local;

import android.content.Context;

import com.futurelight.recipes.dao.EntityFavorite;
import com.futurelight.recipes.dao.EntityResult;
import com.futurelight.recipes.dao.RecipesDao;
import com.futurelight.recipes.dao.RecipesDatabase;
import com.futurelight.recipes.model.Result;

import java.util.List;

public class LocalRecipesRepository {
    private static LocalRecipesRepository instance;
    private final Context context;
    private final RecipesDao dao;


    private LocalRecipesRepository(Context context){
        this.context = context;
        RecipesDatabase database = RecipesDatabase.getInstance(context);
        dao = database.getDao();
    }

    /**
     * 单例模式创建本地数据库访问仓库
     * @param context
     * @return
     */
    public static LocalRecipesRepository getInstance(Context context){
        if (instance == null){
            synchronized (LocalRecipesRepository.class){
                if (instance == null){
                    instance = new LocalRecipesRepository(context);
                }
            }
        }
        return instance;
    }


    /**
     * 获取存在数据库中的食谱列表
     * @param type
     * @return
     */
    public List<EntityResult> getEntityRecipes(String type){
        return dao.getResults(type);
    }

    /**
     * 获取喜欢列表
     * @return
     */
    public List<EntityResult> getFavorites(){
        return dao.getFavorites();
    }

    /**
     * 插入喜欢列表
     * @param entityFavorite
     */
    public void insertFavorite(EntityFavorite ... entityFavorite){
        dao.insertFavorite(entityFavorite);
    }

    /**
     * 插入食谱
     * @param entityResults
     */
    public void insertResult(EntityResult ... entityResults){
        dao.insertResult(entityResults);
    }

    /**
     * 删除喜欢
     * @param entityFavorite
     */
    public void deleteFavorite(EntityFavorite ... entityFavorite){
        dao.deleteFavorite(entityFavorite);
    }

    /**
     * 查找喜欢
     * @param id
     * @return
     */
    public EntityFavorite findFavorite(int id){
       return dao.findFavorite(id);
    }
}
